﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CommonControl;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmClient : XtraForm
    {
        public FrmClient()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmClient_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            var bll = new DMZS.BLL.Client();
            try
            {
                grdClient.DataSource = bll.GetListForUIExpand(string.Empty).Tables[0];
            }
            catch (Exception ex)
            {
                Utils.WriteTxtLog(ex);
                XtraMessageBox.Show(Utils.GetRuntimeMessage(ex), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmClient_Editor();
            frm.ID = 0;
            frm.ShowDialog();
        }

        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvClient.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmClient_Editor();
            frm.ID = Convert.ToInt32(grvClient.GetFocusedDataRow()["ID"]);
            frm.ShowDialog();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvClient.GetFocusedDataRow() == null)
            {
                return;
            }
            if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                var bll = new DMZS.BLL.Client();
                bll.Delete(Convert.ToInt32(grvClient.GetFocusedDataRow()["ID"]));
                LoadData();
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void grvClient_DoubleClick(object sender, EventArgs e)
        {
            if (grvClient.GetFocusedDataRow() == null)
            {
                return;
            }
            var frm = new FrmClient_Editor();
            frm.ID = Convert.ToInt32(grvClient.GetFocusedDataRow()["ID"]);
            frm.ShowDialog();
        }

        private void grvClient_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvClient.IndicatorWidth = grvClient.IndicatorWidth == -1 ? 40 : grvClient.IndicatorWidth;
            grvClient.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvClient);
        }

        private void grvClient_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "IsStop")
            {
                if (e.Value == DBNull.Value || Convert.ToInt32(e.Value) == 0)
                {
                    e.DisplayText = "启用";
                }
                else
                {
                    e.DisplayText="停用";
                }
            }
        }
    }
}
